﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPop.Mime;
using System.Threading;
using System.Globalization;
using System.Data;
public partial class mailindetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if ((Request.QueryString["DateSent"] != null) && (Request.QueryString["From"] != null))
            {

                string From = HttpUtility.HtmlDecode( HttpUtility.UrlDecode(Request.QueryString["From"]));
                string DateSent = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(Request.QueryString["DateSent"]));
                
                mypopclient dd = new mypopclient();
                dd.Connect();

                DataTable tbl = dd.GetAllMesseges();
               string  MessegeID = "";
               foreach (DataRow row in tbl.Rows)
               {
                   if (row["DateSent"].ToString() == DateSent && row["From"].ToString() == From)
                   {
                       MessegeID = row["MessageNumber"].ToString();
                       break;
                   }
               }
               mail1.Msgid = MessegeID;
                
                OpenPop.Mime.Message message = dd.GetMessage(int.Parse(MessegeID));
                mail1.Body = HttpUtility.HtmlEncode(message.FindFirstPlainTextVersion().GetBodyAsText());
                mail1.From = message.Headers.From.ToString();
                mail1.Address = message.Headers.Subject;
                mail1.Date = message.Headers.DateSent.ToString();
                mail1.Importance = message.Headers.Importance.ToString();
               
                if (message.FindAllAttachments().Count > 0)
                {
                    foreach (MessagePart at in message.FindAllAttachments())
                    {
                        LinkButton hl = new LinkButton();
                        hl.Click += new EventHandler(mail1.download_Click);

                        hl.Text = at.FileName;
                        mail1.FindControl("attachments").Controls.Add(hl);
                    }
                }
            }
            else
            {
                mail1.Msgid = "7";
            
            }
        }
       
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;



            }
        }

    }
}
